# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import logging
from datetime import datetime
from dateutil.relativedelta import relativedelta
from operator import itemgetter
import time

import openerp
from openerp import SUPERUSER_ID, api
from openerp import tools
from openerp.osv import fields, osv, expression
from openerp.tools.translate import _
from openerp.tools.float_utils import float_round as round

import openerp.addons.decimal_precision as dp

_logger = logging.getLogger(__name__)


class account_account(osv.osv):
    _inherit = "account.account"

    _columns = {
        'bank_account': fields.boolean('Bank Account'),
        'account_code': fields.char('Code', select=True),
        'account_dept_id': fields.many2one('account.dept', 'Department'),
        'account_cost_center_id': fields.many2one('account.cost.center', 'Cost Center'),
    }

    _defaults = {
        'bank_account': False,
    }
    
    def create(self, cr, uid, vals, context=None):
        full_code = ''
        
        if 'account_code' in vals:
            full_code = vals['account_code']
        if 'account_dept_id' in vals:
            dept_code = self.pool.get('account.dept').browse(cr, uid, vals['account_dept_id']).code or ''
            full_code += ' - ' + dept_code
        if 'account_cost_center_id' in vals:
            cost_center_code = self.pool.get('account.cost.center').browse(cr, uid, vals['account_cost_center_id']).code or ''
            full_code += ' - ' + cost_center_code
        
        vals.update({'code': full_code})
        
        result = super(account_account, self).create(cr, uid, vals, context=context)
        return result

    def _check_allow_code_change(self, cr, uid, ids, context=None):
        line_obj = self.pool.get('account.move.line')
        for account in self.browse(cr, uid, ids, context=context):
            account_ids = self.search(cr, uid, [('id', 'child_of', [account.id])], context=context)
#             if line_obj.search(cr, uid, [('account_id', 'in', account_ids)], context=context):
#                 raise osv.except_osv(_('Warning !'), _("You cannot change the code of account which contains journal items!"))
        return True

    def write(self, cr, uid, ids, vals, context=None):
        full_code = acc_code = acc_dept = acc_cost_center = ''
        account = self.pool.get('account.account').browse(cr, uid, ids[0], context=None)
        
        if 'account_code' in vals:
            acc_code = vals['account_code']
        else:
            acc_code = account.account_code
            
        if 'account_dept_id' in vals:
            if vals['account_dept_id'] != False:
                dept_code = self.pool.get('account.dept').browse(cr, uid, vals['account_dept_id']).code or ''
                acc_dept = dept_code
            else:
                acc_dept = '' 
        else:
            acc_dept = account.account_dept_id.code or ''
        
        if 'account_cost_center_id' in vals:
            if vals['account_cost_center_id'] != False:
                cost_center_code = self.pool.get('account.cost.center').browse(cr, uid, vals['account_cost_center_id']).code or ''
                acc_cost_center = cost_center_code
            else:
                acc_cost_center = ''            
        else:
            acc_cost_center = account.account_cost_center_id.code or ''
        
        full_code = acc_code + ' - ' + acc_dept + ' - ' + acc_cost_center
        
        vals.update({'code': full_code})
            
        return super(account_account, self).write(cr, uid, ids, vals, context=context)
    
class account_dept(osv.osv):
    _name = 'account.dept'
    _description = 'Account Department Code'

    _columns = {
        'name': fields.char('Name', required=True, select=True),
        'code': fields.char('Code', size=64, required=True, select=1),
    }

class account_cost_center(osv.osv):
    _name = 'account.cost.center'
    _description = 'Account Cost Center Code'

    _columns = {
        'name': fields.char('Name', required=True, select=True),
        'code': fields.char('Code', size=64, required=True, select=1),
    }
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
